package br.com.caelum.lojavirtual.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	private static SessionFactory factory;
	private static ThreadLocal<Session> sessions;

	static{
		Configuration conf = new AnnotationConfiguration();
		conf.configure();
		factory = conf.buildSessionFactory();
		sessions = new ThreadLocal<Session>();
	}
	
	private static Session openSession() {
		return factory.openSession();
	}
	public static Session getSession(){
		sessions.set(openSession());
		return sessions.get();
	}
}
